package org.lc.stk.dao.baseInfo;

import java.util.List;
import java.util.Optional;

import org.lc.stk.model.baseInfo.Cengci;
import org.lc.stk.model.baseInfo.Major;
import org.lc.stk.model.baseInfo.MajorCengci;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface MajorCengciRepository extends JpaRepository<MajorCengci, Integer>, JpaSpecificationExecutor<MajorCengci> {
    
    /**
     * 检查专业和培养层次的关联是否已存在
     * @param major 专业
     * @param cengci 培养层次
     * @return Optional<MajorCengci>
     */
    Optional<MajorCengci> findByMajorAndCengci(Major major, Cengci cengci);

    /**
     * 获取专业的所有培养层次关联
     * @param majorId 专业ID
     * @return List<MajorCengci>
     */
    List<MajorCengci> findByMajorIdOrderByOrderNumAsc(Integer majorId);

    /**
     * 获取培养层次的所有专业关联
     * @param cengciId 培养层次ID
     * @return List<MajorCengci>
     */
    List<MajorCengci> findByCengciIdOrderByOrderNumAsc(Integer cengciId);

    /**
     * 批量删除专业的培养层次关联
     * @param majorId 专业ID
     * @return 删除的记录数
     */
    @Modifying
    @Query("DELETE FROM MajorCengci mc WHERE mc.major.id = :majorId")
    int deleteByMajorId(@Param("majorId") Integer majorId);

    /**
     * 批量删除培养层次的专业关联
     * @param cengciId 培养层次ID
     * @return 删除的记录数
     */
    @Modifying
    @Query("DELETE FROM MajorCengci mc WHERE mc.cengci.id = :cengciId")
    int deleteByCengciId(@Param("cengciId") Integer cengciId);

    /**
     * 检查专业的培养层次关联数量
     * @param majorId 专业ID
     * @return long
     */
    long countByMajorId(Integer majorId);

    /**
     * 检查培养层次的专业关联数量
     * @param cengciId 培养层次ID
     * @return long
     */
    long countByCengciId(Integer cengciId);
}